package com.caidan.security.web;

import com.caidan.security.domain.entity.UmsSysUser;
import com.caidan.security.mapper.UmsSysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class UmsSysUserDetailsService implements UserDetailsService {

    @Autowired
    private UmsSysUserMapper umsSysUserMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        查询的用户的信息
        log.info("查询的用户姓名{}", username);
//        UmsSysUser ums = umsSysUserMapper.selectOne(new LambdaQueryWrapper<UmsSysUser>().eq(UmsSysUser::getUsername,username));
        UmsSysUser ums = umsSysUserMapper.queryByUserName(username);
        log.info("输出内容{}", ums);
        return ums;
    }
//    认证角色以及权限
//    @Override
//    public UserDetails loadUserByUsername(String username) throws
//            UsernameNotFoundException {
//// 做用户信息查询，不要多次访问数据库，尽量一次查出需要的数据，多表查询不要超过3张表
//// 1、查询用户的角色信息
//        UmsSysUser umsSysUser = sysUserMapper.selectUserByUsername(username);
//        log.info("没有权限===>{}", umsSysUser);
//// 2、查询用户的权限信息
//        ShiTian
//                查询角色sql
//        if (umsSysUser != null) {
//            Set<UmsRole> roleSet = umsSysUser.getRoleSet();
//// 存储角色id，进行批量查询，不要在for循环中查询数据库
//            Set<Long> roleIds = new HashSet<>(roleSet.size());
//// 获取用户的权限列表
//            Set<String> perms = umsSysUser.getPerms();
//            for (UmsRole umsRole : roleSet) {
//                roleIds.add(umsRole.getRoleId());
//            }
//// 权限查询
//            Set<UmsMenu> menus = menuMapper.selectMenuByRoleId(roleIds);
//            for (UmsMenu menu : menus) {
//                String perm = menu.getPerms();
//// 添加用户权限到set中
//                perms.add(perm);
//            }
//            log.info("有权限====》{}", umsSysUser);
//        }
//        return umsSysUser;
//    }
}
